package com.framework.utils.LeShuaPost;


import com.framework.utils.LeShuaPost.CustomerRegistBeans.*;
import com.framework.utils.MD5Util;
import com.framework.utils.MapObjUtils;
import com.framework.utils.string.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.framework.utils.string.StringUtil.getRandomNumberString;

/**
 * 商户进件
 */
@SuppressWarnings("ALL")
public class CustomerRegist {

    //private static final String aNo="8360308675";
    //test aNo
    private static final String aNo="8357238852";

    private static final String version="1.0";
    private static final String baseUrl="http://capi.lepass.cn";
    //private static final String key="";
    //test key
    private static final String key="testkeyvalue";

    /**
     * 商户进件
     * @param baseInfo
     * @param companyInfo
     * @param info
     * @param customerLicenseInfo
     * @param customerRateInfo
     * @param identityInfo
     * @param settleAccountInfo
     */
    public static void customerRegist(CustomerBaseInfo baseInfo, CompanyInfo companyInfo,
                               CustomerImgInfo info, CustomerLicenseInfo customerLicenseInfo,
                               CustomerRateInfo customerRateInfo,IdentityInfo identityInfo,SettleAccountInfo settleAccountInfo){

        String registUrl=baseUrl+"/customer/regist";

        SortedMap<String,Object> map=new TreeMap<>();
        String nonceStr=StringUtil.getDateString2(8);
        try {
            map.putAll(MapObjUtils.objToMap(baseInfo));
            map.putAll(MapObjUtils.objToMap(companyInfo));
            map.putAll(MapObjUtils.objToMap(info));
            map.putAll(MapObjUtils.objToMap(customerLicenseInfo));
            map.putAll(MapObjUtils.objToMap(customerRateInfo));
            map.putAll(MapObjUtils.objToMap(identityInfo));
            map.putAll(MapObjUtils.objToMap(settleAccountInfo));
            map.put("aNo",aNo);
            map.put("version",version);
            map.put("nonceStr",nonceStr);
            List<NameValuePair> list = new LinkedList<>();
            Set<String> keys = map.keySet();
            StringBuilder sb=new StringBuilder();
            for (String key:keys) {
                if(map.get(key)!=null){
                    sb.append(key).append("=").append(map.get(key).toString()).append("&");
                    list.add(new BasicNameValuePair(key, map.get(key).toString()));
                }
            }
            String params=sb.toString();
            System.out.println(params);
            String signMessage = params +"key=" + key;

            byte[] enc = Base64.encodeBase64(MD5Util.MD5(signMessage).getBytes(StandardCharsets.UTF_8));
            String sign=new String(enc);

            list.add(new BasicNameValuePair("sign", sign));

            HttpPost post = new HttpPost(registUrl);
            //StringEntity entity = new StringEntity(param, "UTF-8");

            UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
            System.out.println(entityParam);
            post.setEntity(entityParam);
            post.addHeader("User-Agent", "Mozilla/5.0 (Windows;  U; Windows NT 5.1; en-US; rv:1.7.6)");
            post.addHeader("Content-Type", "application/x-www-form-urlencoded");

            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response=null;
            String body = null;
            HttpEntity entity1 = null;
            response = httpClient.execute(post);

            entity1 = response.getEntity();
            body = EntityUtils.toString(entity1, "UTF-8");

            System.out.println(body);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }


    }

    /**
     * 请求报文
     * @return
     * @throws IOException
     */
    public static String validateMessage() throws IOException {
        String params="aNo="+aNo;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String str = sdf.format(new Date()) + getRandomNumberString(8);
        params+="&nonceStr="+str;
        params+="&version="+version;
        params+="&key="+key;
        String md5 = MD5Util.MD5(params);
        byte[] enc = Base64.encodeBase64(md5.getBytes(StandardCharsets.UTF_8));
        String sign=new String(enc);
        System.out.println(sign);
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("aNo", aNo);
        BasicNameValuePair param2 = new BasicNameValuePair("version", version);
        BasicNameValuePair param3 = new BasicNameValuePair("nonceStr", str);
        BasicNameValuePair param4 = new BasicNameValuePair("sign", sign);
        list.add(param1);
        list.add(param2);
        list.add(param3);
        list.add(param4);

        HttpPost post = new HttpPost(baseUrl);
        //StringEntity entity = new StringEntity(param, "UTF-8");

        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        System.out.println(entityParam);
        post.setEntity(entityParam);
        post.addHeader("User-Agent", "Mozilla/5.0 (Windows;  U; Windows NT 5.1; en-US; rv:1.7.6)");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response=null;
        String body = null;
        HttpEntity entity1 = null;
        response = httpClient.execute(post);

        entity1 = response.getEntity();
        body = EntityUtils.toString(entity1, "UTF-8");
        return body;
    }

    /**
     * 商户详情
     * @return
     * @throws IOException
     */
    public static String queryCustomerInfo(String customerNo) throws IOException {
        String params="aNo="+aNo;
        params+="&customerNo="+customerNo;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String str = sdf.format(new Date()) + getRandomNumberString(8);
        params+="&nonceStr="+str;
        params+="&version="+version;
        params+="&key="+key;
        String md5 = MD5Util.MD5(params);
        byte[] enc = Base64.encodeBase64(md5.getBytes(StandardCharsets.UTF_8));
        String sign=new String(enc);
        System.out.println(sign);
        List<NameValuePair> list = new LinkedList<>();
        BasicNameValuePair param1 = new BasicNameValuePair("aNo", aNo);
        BasicNameValuePair param2 = new BasicNameValuePair("version", version);
        BasicNameValuePair param3 = new BasicNameValuePair("customerNo", customerNo);
        BasicNameValuePair param4 = new BasicNameValuePair("nonceStr", str);
        BasicNameValuePair param5 = new BasicNameValuePair("sign", sign);
        list.add(param1);
        list.add(param2);
        list.add(param3);
        list.add(param4);
        list.add(param5);
        String queryCustomerInfoUrl = baseUrl + "/customer/queryCustomerInfo";
        HttpPost post = new HttpPost(queryCustomerInfoUrl);
        //StringEntity entity = new StringEntity(param, "UTF-8");

        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        System.out.println(entityParam);
        post.setEntity(entityParam);
        post.addHeader("User-Agent", "Mozilla/5.0 (Windows;  U; Windows NT 5.1; en-US; rv:1.7.6)");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response=null;
        String body = null;
        HttpEntity entity1 = null;
        response = httpClient.execute(post);

        entity1 = response.getEntity();
        body = EntityUtils.toString(entity1, "UTF-8");
        return body;
    }

    public static void main(String[] args) {
        CustomerBaseInfo baseInfo=new CustomerBaseInfo();

        CompanyInfo companyInfo=new CompanyInfo();
        CustomerImgInfo info=new CustomerImgInfo();
        CustomerLicenseInfo customerLicenseInfo=new CustomerLicenseInfo();
        CustomerRateInfo customerRateInfo=new CustomerRateInfo();
        IdentityInfo identityInfo=new IdentityInfo();
        SettleAccountInfo settleAccountInfo=new SettleAccountInfo();

        customerRegist(baseInfo,companyInfo,info,customerLicenseInfo,customerRateInfo,identityInfo,settleAccountInfo);

    }


}
